package com.wsd.bookkeeping.controller;

import com.wsd.bookkeeping.entity.Record;
import com.wsd.bookkeeping.entity.query.IncomeListQuery;
import com.wsd.bookkeeping.entity.query.OutcomeListQuery;
import com.wsd.bookkeeping.entity.query.RecordQuery;
import com.wsd.bookkeeping.service.AccountUserService;
import com.wsd.bookkeeping.service.RecordService;
import com.wsd.bookkeeping.vo.UserIncomeListVO;
import com.wsd.common.constant.AccountConstant;
import com.wsd.common.entity.ResponseEntity;
import com.wsd.common.utils.ResponseUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/record")
public class RecordController {

    @Resource
    private RecordService recordService;

    @Resource
    private AccountUserService accountUserService;

    /**
     * 保存记录
     * @param record
     * @return
     */
    @PostMapping("/")
    public ResponseEntity save(@RequestBody Record record) {
        return ResponseUtils.ret(recordService.save(record));
    }

    /**
     * 根据ID删除记录
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity deleteById(@PathVariable("id") Integer id) {
        return ResponseUtils.ret(recordService.removeById(id));
    }

    /**
     * 修改账单信息
     * @param record
     * @return
     */
    @PutMapping("/")
    public ResponseEntity update(@RequestBody Record record) {
        return ResponseUtils.ret(recordService.updateById(record));
    }

    /**
     * 修改私密状态
     * @param id
     * @return
     */
    @PutMapping("/{id}/secret/{status}")
    public ResponseEntity changeSecretStatus(@PathVariable Integer id, @PathVariable Boolean status) {
        return ResponseUtils.ret(recordService.updateById(Record.getSecretInstance(id, status)));
    }

    /**
     * 根据记录ID查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity getRecordById(@PathVariable("id") Integer id) {
        return ResponseEntity.success().put("record", recordService.queryByRecordId(id));
    }

    /**
     * 分页查询
     * 查询分页数据
     */
    @GetMapping("/{bookId}/pn/{curPage}/le/{date}")
    public ResponseEntity getPage(RecordQuery recordQuery) {
        List<Map<String, Object>> page = recordService.queryPage(recordQuery);
        return ResponseEntity.success().put("page", page);
    }

    /**
     * 根据日期查询收入支出总额
     * @param bookId
     * @param date
     * @return
     */
    @GetMapping("/amount/{bookId}/{date}")
    public ResponseEntity getAmountByDate(@PathVariable("bookId") Integer bookId, @PathVariable("date") String date) {
        List<Map<String, Object>> amountMap = recordService.queryAmountByDate(bookId, date);
        return ResponseEntity.success().put("amount", amountMap);
    }


    /**
     * 根据bookId分页查询收入列表
     * @return
     */
    @GetMapping("/{bookId}/income/pn/{curPage}")
    public ResponseEntity getIncomeList(IncomeListQuery incomeListQuery) {
        return ResponseEntity.success().put("list", recordService.queryIncomeList(incomeListQuery));
    }

    /**
     * 获取用户具体的收入列表
     * @param incomeListQuery 查询参数
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/{bookId}/{userId}/income/pn/{curPage}")
    public ResponseEntity getUserIncomeList(IncomeListQuery incomeListQuery, @PathVariable Integer userId) {
        return ResponseEntity.success().put("list", recordService.queryUserIncomeList(incomeListQuery, userId));
    }

    /**
     * 根据bookId分页查询支出列表
     * @return
     */
    @GetMapping("/{bookId}/outcome/pn/{curPage}")
    public ResponseEntity getOutcomeList(OutcomeListQuery outcomeListQuery) {
        return ResponseEntity.success().put("list", recordService.queryOutcomeList(outcomeListQuery));
    }

    /**
     * 获取用户具体的支出列表
     * @param outcomeListQuery 查询参数
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/{bookId}/{userId}/outcome/pn/{curPage}")
    public ResponseEntity getUserIncomeList(OutcomeListQuery outcomeListQuery, @PathVariable Integer userId) {
        return ResponseEntity.success().put("list", recordService.queryUserOutcomeList(outcomeListQuery, userId));
    }

    /**
     * 图表展示-折线统计图-月收入支出
     * @param bookId
     * @return
     * @throws InterruptedException
     */
    @GetMapping("{bookId}/amount/summary")
    public ResponseEntity getIncomeSummaryByBookId(@PathVariable Integer bookId) throws InterruptedException {
        return ResponseEntity.success().put("list", recordService.queryAccountSummaryByBookId(bookId));
    }

    /**
     * 图表展示-扇形图-收入支出分类排行
     * @param bookId
     * @return
     */
    @GetMapping("{bookId}/rank/summary")
    public ResponseEntity getAccountClassificationRankByBookId(@PathVariable Integer bookId) throws InterruptedException {
        return ResponseEntity.success().put("list", recordService.queryAccountClassificationRankByBookId(bookId));
    }

}
